import 'package:flutter/material.dart';
import 'package:hx_im_plugin/hx_im_plugin.dart';
import 'package:record_amr/record_amr.dart';

class HqChatVoicePlayer extends ChangeNotifier {
  String currentMsgId = '';
  bool isPlaying = false;

  playVoice(EMMessage msg) async {
    if (currentMsgId != null) {
      isPlaying = false;
      notifyListeners();
      currentMsgId = null;
    }

    EMVoiceMessageBody body = msg.body as EMVoiceMessageBody;
    isPlaying = await RecordAmr.play(body.localPath, (path) {
      isPlaying = false;
      notifyListeners();
      currentMsgId = null;
    });
    if (isPlaying) {
      currentMsgId = msg.msgId;
    }
    notifyListeners();
  }

  stopPlay() async {
    if (currentMsgId != null) {
      isPlaying = false;
      notifyListeners();
      currentMsgId = null;
    }

    RecordAmr.stop();
  }
}

